#pragma once

#include <QThread>
#include <QString>

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/time.h>
#include <libswscale/swscale.h>
}

class FFmpegThread : public QThread
{
    Q_OBJECT
public:
    explicit FFmpegThread(QObject *parent = nullptr);
    ~FFmpegThread();
    void open(const QString &filename);
    void close();

    QString fileName() const {
        return filename;
    };

private:
    QString filename; // video file name

    AVFormatContext *formatContext = nullptr;
    AVCodecContext *codecContext = nullptr;
    SwsContext *swsContext = nullptr;
    AVCodec *codec = nullptr;
    AVPacket *packet = nullptr;
    AVFrame *frameRecieve = nullptr;

    int videoStream = -1;
    bool isPlay = false;

signals:
    void getOneFrame(const QImage&); // send one frame to widget

protected:
    void run() override;
};